package com.dxl.lanju.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.dxl.common.model.Page;
import com.dxl.common.utils.SqlBuilder;
import com.dxl.common.utils.Utils;
import com.dxl.dao.common.BaseDao;
import com.lanju.entity.ProductCate;

/**
 * 区域管理DAO
 * @author 邓小林
 */
@Repository
public class ProductCateDao extends BaseDao {
	private static final Logger LOG = Logger.getLogger(ProductCateDao.class);
	
	public Page<ProductCate> list(String keyword, Integer state, Integer pageNo, Integer pageSize) {
		SqlBuilder sb = new SqlBuilder("select * from product_cate where 1 = 1");
		if (Utils.isNotBlank(keyword)) {
			keyword = keyword.trim();
			sb.appendStatement(" and name like ").appendParamValue("%" + keyword + "%");
		}
		if (Utils.isNotNull(state)) {
			sb.appendStatement(" and state = ").appendParamValue(state);
		}
		sb.appendStatement(" order by created_time desc");
		return this.list(ProductCate.class, sb.toStatement(), sb.toParams(), pageNo, pageSize);
	}
	
	public List<ProductCate> listCat(String parent, Integer state) {
		SqlBuilder sb = new SqlBuilder("select * from product_cate where 1 = 1");
		if (Utils.isBlank(parent)) {
			sb.appendStatement(" and pid is null");
		} else {
			sb.appendStatement(" and pid = ").appendParamValue(parent);
		}
		if (Utils.isNotNull(state)) {
			sb.appendStatement(" and state = ").appendParamValue(state);
		}
		sb.appendStatement(" order by name asc");
		return this.list(ProductCate.class, sb.toStatement(), sb.toParams());
	}
	
	public boolean exist(String name) {
		SqlBuilder sb = new SqlBuilder("select * from product_cate where ").appendStatement(" name = ").appendParamValue(name);
		return this.count(sb.toStatement(), sb.toParams()) > 0;
	}
}
